import com.alibaba.fastjson.JSONObject;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.io.FileReader;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * @className: RhinoDemo
 * @description:
 * @author: WANGHUI
 * @createDate: 2018/4/16 15:06
 * @version: 1.0
 */
public class RhinoDemo {

    public static void main(String[] args) throws Exception {
        // FIX BUG1
//        System.out.println("1221121221");

        System.out.println("step1 : " + System.currentTimeMillis() );
        ScriptEngineManager sem = new ScriptEngineManager();
        ScriptEngine se = sem.getEngineByName("javascript");
        System.out.println("step2 : " +  System.currentTimeMillis() );
        //使用上下文，执行eval之后，js的变量和函数都存储于上下文，通过get和put可以获取和保存变量，java和javascript都可以使用同一个上下文
        se.put("myname", "liuxg !!");
        System.out.println(se.get("myname"));

        se.eval("var yourname = 'who are you ?'");
        System.out.println(se.get("yourname"));

        //执行变量
        String script = "var compo = {addr : \"广州\",tel : \"12345678901\"};";
        script += "print(compo.addr + \" : \" + compo.tel)";
        se.eval(script);

        //表达式判断
        script = " myname == 'liuxg11 !!' ";
        Object mynameIsLiuxg = se.eval(script);
        System.out.println(mynameIsLiuxg);
        System.out.println("step3 : " +  System.currentTimeMillis() );
        //
//        Map<String,Object> map = new JSONObject();
//        map.put("name","wanghui");

        String jsonStr = "[ { \"info\" : {\"name\":\"wanghui\"} ,\"addr\" :{\"zipCode\":\"12345\"} } ]" ;
        se.put("jsonObj",JSONObject.parse(jsonStr));
        script = " jsonObj[0].info.name == 'wanghui' ";
        Object mynameIsWanghui = se.eval(script);
        se.eval("print('jsonObj info name is ' +  jsonObj[0].info.name)");
        se.eval("print('jsonObj addr zipCode is ' +  jsonObj[0].addr.zipCode)");
        System.out.println(mynameIsWanghui);

        //执行js函数
        String funscript = "function sum(a,b){return a + b ;}";
        se.eval(funscript);
        Invocable jsInvocable = (Invocable)se;
        Object obj = jsInvocable.invokeFunction("sum", 1,2);
        System.out.println("sum = " + obj);

        //执行js文件
        URL url = RhinoDemo.class.getClassLoader().getResource("rhino.js");//获取classpath下的js文件
        FileReader reader = new FileReader(url.getFile());
        se.eval(reader);
        Object obj1 = jsInvocable.invokeFunction("skyscolor");
        System.out.println(se.get("skycolor") + " -- " + obj1);


         Map<String,Object> map = new JSONObject();
         map.put("condType","wanghui");     map.put("condVal","---xxxxx");

        Object obj2 = jsInvocable.invokeFunction("isTrue",JSONObject.parse(jsonStr),"---xxxxx");
        System.out.println(  " isTrue -- " + obj2);

        //js和java代码混合用
        String jsjavacode =  "var Arrays = Java.type('java.util.Arrays');  var names = Arrays.asList(\"liuxg1\",\"liuxg2\",\"liuxg3\")";
        se.eval(jsjavacode);
        List<String> names = (List<String>) se.get("names");
        for (String name : names) {
            System.out.println(name);
        }

    }
}
